package com.zzc.util.weixin.api;

import com.zzc.util.ApiResult;
import com.zzc.util.MD5Util;
import com.zzc.util.PageData;
import com.zzc.util.Tools;
import com.zzc.util.constant.ContentType;
import com.zzc.util.http.DoHttp;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;


/**
 * @author fengyi on 2017-12-29.
 * 
 */
@Component
public class ErpInter {
	@Value("${erp.api_url}")
	private String erpUrl;
	@Value("${erp.key}")
	private String erpKey;

//	public final String erpUrl = Tools.readTxtFile(Const.WEIXIN_CONFIG).split(",fy,")[2] + "/web/api";

	/**
	 * 获取用户信息
	 * @return
	 * @throws Exception
	 */
	public JSONObject getUser(String openid) throws Exception {

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("vx_id", openid);

		String host = erpUrl + "/web/api/user/wx_get";

		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Content-Type", ContentType.CONTENT_TYPE_JSON);
		headers.put("sign", MD5Util.encodeMD5(jsonObject.toString() + erpKey));

		return JSONObject.fromObject(DoHttp.sendPost(host, jsonObject.toString(), headers));
	}

	/**
	 * 注册
	 * @param user_weixin
	 * @return
	 * @throws Exception
	 */
	public JSONObject register(JSONObject user_weixin, String name,String csrq, String sjh) throws Exception {

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("vx_id", user_weixin.get("openid"));
		jsonObject.put("name", name);
		jsonObject.put("xb", user_weixin.get("sex").equals("2") ? "女" : "男");
		jsonObject.put("dz", user_weixin.get("country").toString() + user_weixin.get("city"));
		jsonObject.put("csrq", csrq.toString().replaceAll("/", "-"));
		jsonObject.put("sjh", sjh);
		String host = erpUrl + "/web/api/user/wx_register";

		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Content-Type", ContentType.CONTENT_TYPE_JSON);
		headers.put("sign", MD5Util.encodeMD5(jsonObject.toString() + erpKey));

		return JSONObject.fromObject(DoHttp.sendPost(host, jsonObject.toString(), headers));
	}
	/**
	 * 修改会员资料
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	public JSONObject upUser(PageData pd) throws Exception {

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("vx_id", pd.get("openid"));
		jsonObject.put("name", pd.get("name"));
		jsonObject.put("xb", pd.get("xb"));
		jsonObject.put("dz", pd.get("dz") == null ? "" : pd.get("dz"));
		jsonObject.put("csrq", pd.get("csrq").toString().replaceAll("/", "-"));
		jsonObject.put("sjh", pd.get("sjh"));
		jsonObject.put("email", pd.get("email") == null ? "" : pd.get("email"));
		jsonObject.put("dq", pd.get("dq") == null ? "" : pd.get("dq"));
		String host = erpUrl + "/web/api/user/wx_up";

		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Content-Type", ContentType.CONTENT_TYPE_JSON);
		headers.put("sign", MD5Util.encodeMD5(jsonObject.toString() + erpKey));

		return JSONObject.fromObject(DoHttp.sendPost(host, jsonObject.toString(), headers));
	}

	/**
	 * 绑定会员卡
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	public JSONObject bind(PageData pd) throws Exception {

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("vx_id", pd.get("openid"));
		jsonObject.put("kh", pd.get("kh"));
		jsonObject.put("mm", pd.get("mm"));
		jsonObject.put("sjh", pd.get("sjh"));
		String host = erpUrl + "/web/api/user/wx_bind";

		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Content-Type", ContentType.CONTENT_TYPE_JSON);
		headers.put("sign", MD5Util.encodeMD5(jsonObject.toString() + erpKey));

		return JSONObject.fromObject(DoHttp.sendPost(host, jsonObject.toString(), headers));

	}

	/**
	 * 验证码发送
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	public JSONObject sendMsg(PageData pd) throws Exception {

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("msg", pd.get("randomNum"));
		jsonObject.put("sjh", pd.get("sjh"));
		String host = erpUrl + "/web/api/message/sendVerificationCode";
		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Content-Type", ContentType.CONTENT_TYPE_JSON);
		headers.put("sign", MD5Util.encodeMD5(jsonObject.toString() + erpKey));

		return JSONObject.fromObject(DoHttp.sendPost(host, jsonObject.toString(), headers));

	}

	/**
	 * 获取积分记录
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	public JSONObject getIntegralDetails(PageData pd) throws Exception {

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("vx_id", pd.get("openid"));
		jsonObject.put("currentPage", pd.get("currentPage"));
		jsonObject.put("pageSize", pd.get("pageSize"));

		String host = erpUrl + "/web/api/integral/wx_list";

		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Content-Type", ContentType.CONTENT_TYPE_JSON);
		headers.put("sign", MD5Util.encodeMD5(jsonObject.toString() + erpKey));

		return JSONObject.fromObject(DoHttp.sendPost(host, jsonObject.toString(), headers));

	}

	/**
	 * 查询优惠券
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	public JSONObject getCouponList(PageData pd) throws Exception {
		String host = erpUrl + "/web/api/coupon/wx_list";
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("vx_id", pd.get("openid"));
		jsonObject.put("currentPage", pd.get("currentPage"));
		jsonObject.put("pageSize", pd.get("pageSize"));
		jsonObject.put("is_sy", pd.get("is_sy"));

		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Content-Type", ContentType.CONTENT_TYPE_JSON);
		headers.put("sign", MD5Util.encodeMD5(jsonObject.toString() + erpKey));

		return JSONObject.fromObject(DoHttp.sendPost(host, jsonObject.toString(), headers));

	}

	/**
	 * 获取消费记录
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	public JSONObject getOrders(PageData pd) throws Exception {
		String host = erpUrl + "/web/api/order/wx_list";
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("vx_id", pd.get("openid"));
		jsonObject.put("currentPage", pd.get("currentPage"));
		jsonObject.put("pageSize", pd.get("pageSize"));

		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Content-Type", ContentType.CONTENT_TYPE_JSON);
		headers.put("sign", MD5Util.encodeMD5(jsonObject.toString() + erpKey));

		return JSONObject.fromObject(DoHttp.sendPost(host, jsonObject.toString(), headers));

	}

	/**
	 * 获取 单笔消费记录明细
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	public JSONObject getOrderDetails(PageData pd) throws Exception {
		String host = erpUrl + "/web/api/order/getDetail";
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("djid", pd.get("djid"));

		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Content-Type", ContentType.CONTENT_TYPE_JSON);
		headers.put("sign", MD5Util.encodeMD5(jsonObject.toString() + erpKey));

		return JSONObject.fromObject(DoHttp.sendPost(host, jsonObject.toString(), headers));

	}
	/**
	 * 获取附近门店
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	public JSONObject getLocal(PageData pd) throws Exception {
		String host = erpUrl + "/web/api/store/list";
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("lat", pd.get("lat"));
		jsonObject.put("lng", pd.get("lng"));

		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Content-Type", ContentType.CONTENT_TYPE_JSON);
		headers.put("sign", MD5Util.encodeMD5(jsonObject.toString() + erpKey));

		return JSONObject.fromObject(DoHttp.sendPost(host, jsonObject.toString(), headers));

	}
	/**
	 * 获取签到详情
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	public JSONObject getSignDetail(PageData pd) throws Exception {
		String host = erpUrl + "/web/api/sign/wx_list";
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("vx_id", pd.get("openid"));

		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Content-Type", ContentType.CONTENT_TYPE_JSON);
		headers.put("sign", MD5Util.encodeMD5(jsonObject.toString() + erpKey));

		return JSONObject.fromObject(DoHttp.sendPost(host, jsonObject.toString(), headers));

	}
	/**
	 * 签到
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	public JSONObject sign(PageData pd) throws Exception {
		String host = erpUrl + "/web/api/sign/wx_doSign";
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("vx_id", pd.get("openid"));

		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Content-Type", ContentType.CONTENT_TYPE_JSON);
		headers.put("sign", MD5Util.encodeMD5(jsonObject.toString() + erpKey));

		return JSONObject.fromObject(DoHttp.sendPost(host, jsonObject.toString(), headers));

	}
	public static void  main(String[] args) throws Exception {
	    JSONObject jsonObject = new JSONObject();
        jsonObject.put("msg", Tools.getRandomNum() + "");
        jsonObject.put("sjh", "15708438296");
	    Map<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "application/json; charset=UTF-8");
        headers.put("sign", MD5Util.encodeMD5(jsonObject.toString() + "B7E24503D67D96F13A60C167EF062D1C"));
        
	    long start = System.currentTimeMillis();
        System.out.println(JSONObject.fromObject(DoHttp.sendPost("http://222.209.88.12:8697/tcxc/vipInterface.do?action=sendMsg", jsonObject.toString(), headers)));
        System.out.println(System.currentTimeMillis() - start);
    }
}
